package com.zjhd.sealcutting.controller;

import com.zjhd.sealcutting.domain.MemberSessionid;
import com.zjhd.sealcutting.domain.UserInfo;
import com.zjhd.sealcutting.param.LoginParam;
import com.zjhd.sealcutting.service.LoginService;
import com.zjhd.sealcutting.service.MemberSessionidService;
import com.zjhd.sealcutting.utils.CookieUtil;
import com.zjhd.sealcutting.utils.OperLog;
import com.zjhd.sealcutting.utils.TokenUtil;
import com.zjhd.sealcutting.utils.response.ResponseResult;
import com.zjhd.sealcutting.utils.response.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * TODO  用户登录
 *
 * @author wangchao
 * @version 1.0
 * @date 2020/11/23 10:15
 */
@Api(value = "用户登录", tags = "用户登录")
@RequestMapping("/login")
@RestController
public class LoginController {

    private final static Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private LoginService loginService;

    @Autowired
    private MemberSessionidService memberSessionidService;


    @ApiOperation(value = "登录接口", notes = "登录接口")
    @PostMapping("/signIn")
    @OperLog(operModul = "登录管理-登录",operType = "查看",operDesc = "用户登录")
    public ResponseResult<UserInfo> signIn(HttpServletRequest request, HttpServletResponse response, @RequestBody LoginParam param) throws Exception{
        try {
            if(param==null){
                ResultGenerator.genFailResult("登录请求参数不能为空");
            }
            return loginService.signIn(request,response,param);
        }catch (Exception ex){
            logger.info("登录接口处理异常："+ex,this.getClass());
        }
        return ResultGenerator.genFailResult("登录失败");
    }

    @ApiOperation(value = "验证用户登录手机号是否存在", notes = "验证用户登录手机号是否存在")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile",value = "手机号",dataType = "String" ,paramType = "query")
    })
    @GetMapping("/checkMobile")
    public ResponseResult<Boolean> checkMobile(String mobile) throws Exception{
        try {
            if(StringUtils.isBlank(mobile)){
                ResultGenerator.genFailResult("登录手机号不能为空!");
            }
            return ResultGenerator.genSuccessResult(loginService.checkMobile(mobile));
        }catch (Exception ex){
            logger.info("验证用户登录手机号是否存在接口处理异常："+ex,this.getClass());
        }
        return ResultGenerator.genFailResult("验证用户登录手机号是否存在接口失败");
    }

    @ApiOperation(value = "根据手机号发送短信验证码", notes = "根据手机号发送短信验证码")
    @PostMapping(value = "/sendMobileSmsVerificationCode")
    public ResponseResult sendMobileSmsVerificationCode(@RequestBody LoginParam param)throws Exception{
        try {
            if(param==null){
                return ResultGenerator.genFailResult("请求参数不能为空");
            }
            return loginService.sendMobileSmsVerificationCode(param);
        }catch (Exception ex){
            logger.info("发送短信处理异常："+ex,this.getClass());
        }
        return ResultGenerator.genFailResult("发送失败");
    }

    @ApiOperation(value = "退出登录", notes = "退出登录")
    @GetMapping(value = "/logout")
    @OperLog(operModul = "登录管理-退出登录",operType = "查看",operDesc = "退出登录")
    public ResponseResult logout(HttpServletRequest request,HttpServletResponse response)throws Exception{
        try {
            //获取token
            if (CookieUtil.get(request,"Authorization") != null) {
                String token = CookieUtil.get(request,"Authorization").getValue();
                if(StringUtils.isNotBlank(token)){
                    UserInfo info = TokenUtil.parseAccessToken(token);
                    if(info!=null){
                        memberSessionidService.deleteByPrimaryKey(info.getId());
                    }
                }
            }
            CookieUtil.del(request,response);
            return ResultGenerator.genSuccessResult("退出成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultGenerator.genFailResult("退出失败");
    }
}
